package com.wms.common.utils;

import org.springframework.stereotype.Component;

@Component
public class SortUtil {
    private static final String TASK_SORT = "task_sort";
    private static final String PLAN_SORT = "plan_sort";
    private static final String PRODUCT_SCHEDULE_SORT = "product_schedule_sort";
    private final CacheService cacheService;

    public SortUtil(CacheService cacheService) {
        this.cacheService = cacheService;
    }


    public static long getNanosecond() {
        return System.currentTimeMillis();
    }

    /**
     * 获取出库计划排序字段
     *
     * @return 排序值
     */
    public long getTskSort() {
        long sort = getSort(TASK_SORT);
        sort++;
        cacheService.set(TASK_SORT, String.valueOf(sort));
        return System.currentTimeMillis() + sort;
    }

    public long getPlanSort() {
        long sort = getSort(PLAN_SORT);
        sort++;
        cacheService.set(PLAN_SORT, String.valueOf(sort));
        return System.currentTimeMillis() + sort;
    }

    public long getProductScheduleSort() {
        long sort = getSort(PRODUCT_SCHEDULE_SORT);
        sort++;
        cacheService.set(PRODUCT_SCHEDULE_SORT, String.valueOf(sort));
        return System.currentTimeMillis() + sort;
    }


    private Long getSort(String key) {
        String sortStr = cacheService.get(key);
        return StringUtils.isEmpty(sortStr) ? 0L : Long.parseLong(sortStr);
    }

}

